<div class="row animate fadeIn">
    <form class="form-horizontal" id="inv_offcr_edit">
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit</i></h5>
        <div class="ibox-tools">    
             <button class="btn btn-primary btn-sm save-btn-tab" id="submit-invrole">Save</button>
             <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
     </div>
     <div class="ibox-content">
        <?php foreach ($offcr_data as $data) { ?>
            <input type="hidden" name="e_invrole_id" value="<?php echo $data->inv_role_id; ?>">
            <div class="form-group">
                <label class="col-lg-3 control-label">Username:</label>
                <div class="col-lg-5">
                    <input type="text" id="" class="form-control" name="" value="<?php echo $data->user_name; ?>" readonly>
                </div>
             </div>
             <div class="form-group">
                <label class="col-lg-3 control-label">First Name:</label>
                 <div class="col-lg-5">
                    <input type="text" id="" class="form-control" name="" value="<?php echo $data->first_name; ?>" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Last Name:</label>
                 <div class="col-lg-5">
                    <input type="text" id=""class="form-control" name=""  value="<?php echo $data->last_name; ?>" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Inventory Role:</label>
                <div class="col-lg-5">
                    <select class = "chosen-select" name="edit_offcr_role" id ="">
                      <?php foreach ($officer as $offcr) { ?>
                        <option value="<?php echo $offcr->lookup_code; ?>" id="" <?php echo ($offcr->lookup_code == $data->role_code ? 'selected' : ''); ?> ><?php echo $offcr->meaning; ?></option>
                      <?php } ?>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Enable</label>
                <div class="col-lg-5">
                   <input type="checkbox" class="i-checks"  name="edit_offcr_ena" value="ok" <?php echo($data->enable_flag == 'Y' ? 'checked' : ''); ?> >
                </div>
            </div>
          <?php } ?>
      </div>
    </form>


 
    
 </div>
 <script>
    $(document).ready(function(){
    $('#inv_offcr_edit').data('serialize',$('#inv_offcr_edit').serialize());
    $(".chosen-select").chosen({width:"100%;"}); 

     $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
              });



     $('#inv_role_un').change(function () {
      $.ajax({
             type:  "POST",
             url:   "<?php echo base_url($this->session->userdata('forajax').'/get_usrinfo'); ?>",
             data:  { "user_id" : $('#inv_role_un').val()},
             dataType: 'json',
             success : function(data){
              console.log(data[0].first_name);
                $('#inv_fn').val(data[0].first_name);
                $('#inv_ln').val(data[0].last_name);
             },
             beforeSend : function(){
                $('#inv_role_un').attr('disabled',true);
             },
             complete : function(){
                $('#inv_role_un').removeAttr('disabled');
             }
          });

     });

      $.validator.setDefaults({ ignore: ":hidden:not(select)" });
      $('#inv_offcr_edit').validate({
        rules:{
          edit_offcr_role : {
            required : true
          }
        },submitHandler: function(form){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/update_offcr'); ?>",
                        data: $('#inv_offcr_edit').serialize(),
                        dataType: 'json',
                    success: function(data){
                            console.log(data);
                            if(data[0].error != 1){
                                notifymsg("success",". . .Inventory Officer Updated Successfully");
                                close_edit(); active_search();
                             }else{
                              notifymsg("danger",data[0].msg);
                             }
                           // if(data == "true"){
                           //      notifymsg("success",". . .Inventory Cost Added Successfully");
                           //      close_edit(); active_search();
                           // }else{
                           //  notifymsg("danger","Error, Creation Failed");
                           // }
                        },
                        complete: function() {

                        },
                        beforeSend: function(){
                           
                        }
                    });             
          }
      });






      function alert_cancel(){
            if($('#inv_offcr_edit').serialize()!=$('#inv_offcr_edit').data('serialize')){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_edit(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_edit(); active_search();
                notifymsg("hide","hide");
            }
        } 
        $('#cancel_add').click(function () {
          if($('#cancel_add').html() == "Complete"){
            close_edit(); active_search();
            notifymsg("hide","hide");
          }else{            
            alert_cancel();
          }
        });


      $('#editX').click(function(){
            alert_cancel();      
       });
    });
 </script>  